package cn.jeefast.resthz.webservice.demo;


import cn.jeefast.common.utils.PropertiesUtils;
import com.wondersgroup.cuteinfo.client.exchangeserver.exchangetransport.bo.UEventReceiver;
import com.wondersgroup.cuteinfo.client.exchangeserver.exchangetransport.bo.UEventReceiverPackageUnit;
import com.wondersgroup.cuteinfo.client.exchangeserver.exchangetransport.bo.UEventReceiverUnitSimpleData;
import com.wondersgroup.cuteinfo.client.exchangeserver.exchangetransport.dao.IMessageTransporterDAO;
import com.wondersgroup.cuteinfo.client.exchangeserver.exchangetransport.exception.UMessageTransportException;
import com.wondersgroup.cuteinfo.client.exchangeserver.exchangetransport.factory.ITransportClientFactory;
import com.wondersgroup.cuteinfo.client.exchangeserver.exchangetransport.impl.UConfirmReceiveRequest;
import com.wondersgroup.cuteinfo.client.exchangeserver.exchangetransport.impl.UReceiveRequest;
import com.wondersgroup.cuteinfo.client.exchangeserver.exchangetransport.impl.UReceiveResponse;
import com.wondersgroup.cuteinfo.client.exchangeserver.usersecurty.UserSecurityClient;
import com.wondersgroup.cuteinfo.client.exchangeserver.usersecurty.UserToken;
import com.wondersgroup.cuteinfo.client.util.Base64Util;
import com.wondersgroup.cuteinfo.client.util.FileUtil;
import com.wondersgroup.cuteinfo.client.util.UserTokenUtils;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Properties;

public class NRtest {

	/**
	 * @param args
	 */
	public static void main(String[] args)  {

		//读取配置文件
		Properties properties = PropertiesUtils.PROPERTIES.getProperties("config/client_demo.properties");

		String targetURL = properties.getProperty("targetURL");
		String securityURL = properties.getProperty("securityURL");

		IMessageTransporterDAO transporter=null;
		UserSecurityClient security=null;
		try {
//			security=UserSecurityClientFactory.createUserSecurityClient(securityURL);
//			UserToken user=security.authenticate("zjjh00001", "111111");
//			System.out.println("tokenid:"+user.getTokenID());
//
//
			//第二种方式，调用统一认证的令牌认证服务
			UserToken user = UserTokenUtils.getTicket(properties.getProperty("username"), properties.getProperty("password"), properties.getProperty("resource"), properties.getProperty("authURL"));
			System.out.println("tokenid:"+user.getTokenID());


			transporter=ITransportClientFactory.createMessageTransporter(user, targetURL);
			UReceiveRequest receiveReq=new UReceiveRequest(10);
			receiveReq.setConfirmReceive(false);

			long start = System.currentTimeMillis();

			UReceiveResponse response=transporter.receive(receiveReq);

			try{
				for (int i = 0; i < response.getUEventReceiverList().size(); i++) {
					UEventReceiver event=response.getUEventReceiverList().get(i);

					for(int j=0;j<event.getEventReceiverPackage().getEventReceiverUnitList().size();j++){
						UEventReceiverPackageUnit unit=event.getEventReceiverPackage().getEventReceiverUnitList().get(j);
						if(unit.getEventReceiverUnitSimpleDataList()!=null){
							for (int k = 0; k < unit.getEventReceiverUnitSimpleDataList().size(); k++) {
								UEventReceiverUnitSimpleData simpledata=unit.getEventReceiverUnitSimpleDataList().get(k);
								System.out.println(simpledata.getFieldname()+":"+simpledata.getFieldvalue());
							}
						}
						if (unit.getEventReceiverUnitDataFile()!=null&&unit.getEventReceiverUnitDataFile().getAttachmentData()!=null) {
							byte[] bytes=unit.getEventReceiverUnitDataFile().getAttachmentData();
							String filename=unit.getEventReceiverUnitDataFile().getFilename();
							//						FileUtil.getFileFromBytes(bytes, "c:/aaabb.xml");
							FileUtil.getFileFromBytes(bytes, "d:/"+filename);
						}
						if (unit.getEventReceiverUnitDataFile()!=null&&unit.getEventReceiverUnitDataFile().getBase64EncodedData()!=null) {
							String xml= new String(unit.getEventReceiverUnitDataFile().getBase64EncodedData(),"utf-8");
							xml=new String(Base64Util.getFromBASE64Byte(xml), "utf-8");
							System.err.println("xml==================" + xml);
						}
					}
					long end = System.currentTimeMillis();
					System.out.println("耗时："+(end-start)+"毫秒");
				}
			}catch(Exception e){
				e.printStackTrace();
			}

			if (response.getResponseXMLArray().length>0) {
				for (int i = 0; i < response.getResponseXMLArray().length; i++) {
					System.out.println("1--->"
							+ response.getResponseXMLArray()[i].getResponseXML());
					UConfirmReceiveRequest confirmReq = new UConfirmReceiveRequest();
					confirmReq.setDeleteEvent(true);
					System.out.println();
					confirmReq.setConfirmReceiveRequest(response.getResponseXMLArray()[i].getEventID());
					transporter.confirmReceive(confirmReq);
				}

			}else if(response.getGenericFault()!=null){
				System.out.println("2--->"+response.getGenericFault().getMessage());
			}
//
//		} catch (UserSecurityException e) {
//			e.printStackTrace();
		} catch (UMessageTransportException e) {
			e.printStackTrace();
		}
	}
	public static String getActionTypeToXML() {
		Calendar cc=Calendar.getInstance();
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String xml = "";
		xml += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
		xml = xml + "<root>";
		xml = xml + "<baseInfo>";
		xml = xml + "<shipperName>托运方</shipperName>";
		xml = xml + "<wldm>托运方物流交换代码</wldm>";
		xml = xml + "<acceptTime>2009-11-17 20:20</acceptTime>";
		xml = xml + "<sourceAddress>起始地</sourceAddress>";
		xml = xml + "<unloadAddress>目的地</unloadAddress>";
		xml = xml + "<arriveTime>到达时间</arriveTime>";
		xml = xml + "<opinion>评价信息（默认获用户好评）</opinion>";
		xml = xml + "<sourceSystem>001</sourceSystem>";
		xml = xml + "</baseInfo>";
		xml = xml + "</root>";
		return xml;
	}

}
